package com.xbongbong.pro.statistic.service.impl;

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.EnableEnum;
import com.xbongbong.paas.enums.FundAccountFlowTypeEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.constant.I18nStringConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.statistic.enums.BusinessComboEnum;
import com.xbongbong.pro.statistic.pojo.dto.DefaultComboValueListDTO;
import com.xbongbong.pro.statistic.pojo.dto.FundAccountListDTO;
import com.xbongbong.pro.statistic.pojo.vo.DefaultComboValueListVO;
import com.xbongbong.pro.statistic.pojo.vo.FundAccountListVO;
import com.xbongbong.pro.statistic.service.ChartFilterService;
import com.xbongbong.saas.domain.entity.DefaultComboValueEntity;
import com.xbongbong.saas.domain.entity.FundAccountEntity;
import com.xbongbong.saas.model.FundAccountModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 徐俊杰
 * @version v1.0
 * @date 2020/3/17 17:12
 * @since v1.0
 */
@Service("chartFilterService")
public class ChartFilterServiceImpl implements ChartFilterService {

    @Resource
    private FundAccountModel fundAccountModel;

    @Override
    public FundAccountListVO fundAccountList(FundAccountListDTO fundAccountListDTO) throws XbbException {
        List<FundAccountEntity> homeList = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        homeList.add(new FundAccountEntity(0L, I18nMessageUtil.getMessage(I18nStringConstant.ALL_ACCOUNTS)));
        //装载参数
        String corpid = fundAccountListDTO.getCorpid();
        Map<String, Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        params.put("corpid", corpid);
        params.put("enable", EnableEnum.OPEN.getCode());
        params.put("del", DelEnum.NORMAL.getDel());
        params.put("orderByStr", "add_time asc");
        params.put("columns", "id, name");
        //查询
        homeList.addAll(fundAccountModel.findEntitys(params));
        return new FundAccountListVO(homeList);
    }

    /**
     * 获取统计这边查询时，下拉选的默认值
     *
     * @param defaultComboValueListDTO
     * @return
     */
    @Override
    public DefaultComboValueListVO defaultComboValueList(DefaultComboValueListDTO defaultComboValueListDTO) throws XbbException{
        DefaultComboValueListVO defaultComboValueListVO = new DefaultComboValueListVO();
        List<DefaultComboValueEntity> defaultComboValueEntityList = new ArrayList<>();
        Integer businessTag = defaultComboValueListDTO.getBusinessTag();
        Integer filterConditionCode = defaultComboValueListDTO.getFilterConditionCode();
        BusinessComboEnum businessComboEnum = BusinessComboEnum.getEnumByTagAndCode(businessTag, filterConditionCode);
        switch (businessComboEnum) {
            case FUND_ACCOUNT_INCOME_EXPENDITURE:
                List<FundAccountFlowTypeEnum> fundAccountFlowTypeEnums = FundAccountFlowTypeEnum.getComboValues();
                for (FundAccountFlowTypeEnum fundAccountFlowTypeEnum : fundAccountFlowTypeEnums) {
                    DefaultComboValueEntity defaultComboValueEntity = new DefaultComboValueEntity();
                    defaultComboValueEntity.setId(fundAccountFlowTypeEnum.getCode());
                    defaultComboValueEntity.setName(fundAccountFlowTypeEnum.getName());
                    defaultComboValueEntityList.add(defaultComboValueEntity);
                }
                defaultComboValueListVO.setDefaultComboValueList(defaultComboValueEntityList);
                break;
            default:
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100002);
        }
        return defaultComboValueListVO;
    }
}
